<template>
  <Dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    style="min-width: 1000px"
    scroll
    :max-height="600"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <template class="flex">
        <el-form-item label="计划编号" prop="planCode">
          <el-input
            v-model="formData.planCode"
            placeholder="请输入计划编号"
            class="!w-300px"
            readonly
          />
        </el-form-item>
        <el-form-item label="所属航空公司" prop="airlineComp">
          <el-input
            readonly
            v-model="formData.airlineComp"
            placeholder="请输入所属航空公司"
            class="!w-300px"
          />
        </el-form-item>
      </template>
      <template class="flex">
        <el-form-item label="航班号" prop="flightNo">
          <el-input
            v-model="formData.flightNo"
            placeholder="请输入航班号"
            class="!w-300px"
            readonly
          />
        </el-form-item>
        <el-form-item label="起飞时间" prop="depTime">
          <el-time-picker
            readonly
            v-model="formData.depTime"
            format="HH:mm"
            value-format="HH:mm"
            placeholder="选择起飞时间"
            clearable
            class="!w-300px"
          />
        </el-form-item>
      </template>
      <!-- 商户舱 -->
      <ContentWrap>
        <ProduceRecordFormItem prefix="bussClass" title="商务舱" :model="formData" />
        <ProduceRecordFormItem
          prefix="bussClassSpc"
          title="商务舱"
          :special="true"
          :model="formData"
        />
      </ContentWrap>
      <!-- 经济舱 -->
      <ContentWrap>
        <ProduceRecordFormItem prefix="ecoClass" title="经济舱" :model="formData" />
        <ProduceRecordFormItem
          prefix="ecoClassSpc"
          title="经济舱"
          :special="true"
          :model="formData"
        />
      </ContentWrap>
      <!-- 高级舱 -->
      <ContentWrap>
        <ProduceRecordFormItem prefix="premEcoClass" title="高级舱" :model="formData" />
        <ProduceRecordFormItem
          prefix="premEcoClassSpc"
          title="高级舱"
          :special="true"
          :model="formData"
        />
      </ContentWrap>
      <!-- 机组舱 -->
      <ContentWrap>
        <ProduceRecordFormItem prefix="crew" title="机组舱" :model="formData" />
        <ProduceRecordFormItem prefix="crewSpc" title="机组舱" :special="true" :model="formData" />
      </ContentWrap>
      <el-form-item label="标记特别说明" prop="specialRemarks">
        <el-input
          class="!w-300px"
          readonly
          v-model="formData.specialRemarks"
          :rows="3"
          type="textarea"
          placeholder="请输入对应内容"
        />
      </el-form-item>
      <el-form-item label="总配餐份数" prop="sumQty">
        <el-input
          class="!w-300px"
          placeholder="自动合并计算以上所有总数量"
          v-model="sumQty"
          readonly
        >
          <template #append>份</template>
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <!--<el-button @click="submitForm" type="primary" :readonly="formLoading">确 定</el-button>-->
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ProduceRecordApi, ProduceRecordVO } from '@/api/pplan/producerecord'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import ProduceRecordFormItem from './ProduceRecordFormItem.vue'
import { TablewareApi, TablewareVO } from '@/api/pplan/tableware' // 餐具
import { RecipeApi, RecipeVO } from '@/api/pplan/recipe' // 餐谱
import { RecipeMealApi, RecipeMealVO } from '@/api/pplan/recipemeal' // 菜品

/** 生产计划-生产计划记录 表单 */
defineOptions({ name: 'ProduceRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({})
const formRules = reactive({})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProduceRecordApi.getProduceRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
/** 打开弹窗 */
const openFlightNo = async (type: string, flightNo?: string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (flightNo) {
    formLoading.value = true
    try {
      formData.value = await ProduceRecordApi.getProduceRecordByFN(flightNo)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open, openFlightNo }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProduceRecordVO
    if (formType.value === 'create') {
      await ProduceRecordApi.createProduceRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProduceRecordApi.updateProduceRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const sumQty = computed(() => {
  const toNumber = (value) => Number(value || 0)
  return (
    toNumber(formData.value.bussClassMealQty) +
    toNumber(formData.value.premEcoClassMealQty) +
    toNumber(formData.value.ecoClassMealQty) +
    toNumber(formData.value.crewMealQty) +
    toNumber(formData.value.bussClassSpcMealQty) +
    toNumber(formData.value.premEcoClassSpcMealQty) +
    toNumber(formData.value.ecoClassSpcMealQty) +
    toNumber(formData.value.crewSpcMealQty)
  )
})

/** 重置表单 */
const resetForm = () => {
  Object.keys(formData.value).forEach((key) => {
    formData.value[key] = undefined
  })
  formRef.value?.resetFields()
}
</script>
